﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Collections;
using System.Data;
using NBusiness.Query;

namespace NBusiness.Data.Providers.SqlServer
{
    public class SqlLoader : ILoader
    {
        private IDataReader _dr;

        public SqlLoader(IDataReader dr)
        {
            if (dr == null)
                throw new ArgumentNullException("dr");

            _dr = dr;
        }

        public T GetValue<T>(string column)
        {
            T val = default(T);

            int ordinal = _dr.GetOrdinal(column);
            if (!_dr.IsDBNull(ordinal))
                val = (T)_dr.GetValue(ordinal);

            return val;
        }

        public bool ContainsValue(string column)
        {
            int ordinal = _dr.GetOrdinal(column);
            return ordinal > -1;
        }

        public bool Read()
        {
            return _dr.Read();
        }

        public bool Next()
        {
            return _dr.NextResult();
        }

        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_dr != null)
                {
                    _dr.Dispose();
                    _dr = null;
                }
            }
        }
        #endregion
    }
}
